package android.slc.mp.ui.page;

import android.os.Bundle;
import android.slc.mp.SlcMp;
import android.slc.mp.po.i.IBaseFolder;
import android.slc.mp.po.i.IBaseItem;
import android.slc.mp.po.i.IBaseResult;
import android.slc.mp.ui.SlcIMpDelegate;
import android.slc.mp.ui.page.audio.SlcMpPagerAudioFragment2;
import android.slc.mp.ui.page.file.SlcMpPagerFileFragment2;
import android.slc.mp.ui.page.photo.SlcMpPagerPhotoFragment2;
import android.slc.mp.ui.page.video.SlcMpPagerVideoFragment2;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

/**
 * Created by slc
 * A placeholder fragment containing a simple view.
 */
public abstract class SlcMpPagerBaseFragment2<S extends IBaseResult<F, T>, F extends IBaseFolder<T>, T extends IBaseItem> extends Fragment {

    public final static String POSITION = "position";

    protected SlcMpPagerBaseVd<S, F, T> slcMpPagerBaseVd;

    private View mContentView;
    protected SlcMpPagerVirtualVm<S, F, T> mMpVirtualVm;

    public static SlcMpPagerBaseFragment2<?, ?, ?> newInstance(int mediaType, SlcIMpDelegate slcIMpDelegate) {
        return newInstance(0, mediaType, slcIMpDelegate);
    }

    public static SlcMpPagerBaseFragment2<?, ?, ?> newInstance(int position, int mediaType,
                                                               SlcIMpDelegate slcIMpDelegate) {
        SlcMpPagerBaseFragment2<?, ?, ?> fragment;
        switch (mediaType) {
            case SlcMp.MEDIA_TYPE_PHOTO:
                fragment = new SlcMpPagerPhotoFragment2();
                break;
            case SlcMp.MEDIA_TYPE_VIDEO:
                fragment = new SlcMpPagerVideoFragment2();
                break;
            case SlcMp.MEDIA_TYPE_AUDIO:
                fragment = new SlcMpPagerAudioFragment2();
                break;
            //case SlcMp.MEDIA_TYPE_FILE:
            default:
                fragment = new SlcMpPagerFileFragment2();
                break;
        }
        //此j句暂时用不到
        /*if (fragment == null) {
            throw new IllegalStateException("媒体类型异常，请参考MediaPicker中的媒体类型");
        }*/
        fragment.setMediaPickerDelegate(mediaType, slcIMpDelegate);
        Bundle bundle = new Bundle();
        bundle.putInt(SlcMp.Key.KEY_MEDIA_TYPE, mediaType);
        bundle.putInt(POSITION, position);
        fragment.setArguments(bundle);
        return fragment;
    }

    /**
     * 设置代理器
     *
     * @param mediaType
     * @param slcIMpDelegate
     */
    public void setMediaPickerDelegate(int mediaType, SlcIMpDelegate slcIMpDelegate) {
        this.mMpVirtualVm = getMediaPickerListDelegate(mediaType, slcIMpDelegate);
    }

    protected abstract SlcMpPagerVirtualVm<S, F, T> getMediaPickerListDelegate(int mediaType, SlcIMpDelegate slcIMpDelegate);

    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        slcMpPagerBaseVd = initSlcMpPagerVd();
        slcMpPagerBaseVd.setMpVirtualVm(this.mMpVirtualVm);
        return slcMpPagerBaseVd.onCreateView(requireActivity(), inflater, container, savedInstanceState);
    }

    protected abstract SlcMpPagerBaseVd<S, F, T> initSlcMpPagerVd();

    @Override
    public void onDestroyView() {
        slcMpPagerBaseVd.onDestroyView();
        super.onDestroyView();
    }
}